﻿using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.Options;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace Elasticsearch.Synchronize.Job.Entity
{
    [BsonIgnoreExtraElements(true)]
    public class EventJoin
    {
        [BsonId]
        [BsonRepresentation(BsonType.ObjectId)]
        [Nest.Keyword(Ignore = true)]
        public string _id { get; set; }

        public long JoinId { get; set; }

        public long EventId { get; set; }

        [Nest.Object(Ignore = true)]
        [BsonDictionaryOptions(DictionaryRepresentation.Document)]
        public Dictionary<string, string> ExtraFields { get; set; }

        /// <summary>
        /// ES反序列化时使用此字段
        /// JSON反序列化时使用此字段
        /// 新增和修改数据保存MongoDB时忽略此字段
        /// </summary>
        [BsonIgnore]
        [JsonProperty("ExtraFieldsForES")]
        [Nest.Object(Name = "ExtraFieldsForES")]
        public Dictionary<string, object> ExtraFieldsForES
        {
            get
            {
                if (this.ExtraFieldsForMongo != null)
                {
                    return this.ExtraFieldsForMongo.ToDictionary();
                }

                return new Dictionary<string, object>();
            }
        }

        /// <summary>
        /// 此字段用于新增和修改数据保存MongoDB时使用
        /// mongo-connector会根据此字段把变更部分同步到ES
        /// ES反序列化时忽略此字段
        /// JSON反序列化时忽略此字段
        /// 此字段使用BsonDocument是为了提高数据精度
        /// </summary>
        [JsonIgnore]
        [Nest.Object(Ignore = true)]
        [BsonElement("ExtraFieldsForES")]
        public BsonDocument ExtraFieldsForMongo { get; set; }
    }
}